home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / printing / graytext / graytext.p < prev    next >
Encoding:
Text File  |  2000-06-23  |  6.0 KB  |  219 lines

  1. {
  2.     File:        GrayText.p
  3.  
  4.     Contains:    GrayText demonstrates two methods of creating grayscale text.  On PostScript
  5.                 printers it sends PostScript, on QuickDraw printers it sends Color QuickDraw calls. 
  6.                 If a QuickDraw printer does not use a CGrafPort, the gray text comes out as black.
  7.                 Unfortunately, there's currently no good way to handle that situation.
  8.             
  9.                 Note: I'm assuming Color QuickDraw is present, but the app should really check for
  10.                 that with Gestalt.
  11.  
  12.     Written by: Dave Hersey    
  13.  
  14.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  15.  
  16.                 You may incorporate this Apple sample source code into your program(s) without
  17.                 restriction. This Apple sample source code has been provided "AS IS" and the
  18.                 responsibility for its operation is yours. You are not permitted to redistribute
  19.                 this Apple sample source code as "Apple sample source code" after having made
  20.                 changes. If you're going to re-distribute the source, we require that you make
  21.                 it clear in the source that the code was descended from Apple sample source
  22.                 code, but that you've made changes.
  23.  
  24.     Change History (most recent first):
  25.                 7/26/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  26.                 
  27.  
  28. }
  29.  
  30. PROGRAM GrayText;
  31.  
  32. USES
  33.     Memory, QuickDraw, Traps, Printing, Packages, PrintComments,Fonts;
  34.  
  35.  
  36. {*------ SendPostScript ------------------------------------------------------------*}
  37.  
  38. PROCEDURE SendPostScript(theComment: Str255);
  39. VAR
  40.     PSCommand    : Str255;
  41.     CommandHdl    : Handle;
  42.     CRString    : Str255;
  43.     theError    : OSErr;
  44. BEGIN
  45.     CRString := ' ';
  46.     CRString[1] := CHR(13);
  47.     PSCommand := theComment;
  48.     PSCommand := CONCAT(PSCommand, CRString);
  49.     theError := PtrToHand(POINTER(ORD(@PSCommand) + 1), CommandHdl, LENGTH(PSCommand));
  50.     PicComment(PostScriptHandle, LENGTH(PSCommand), CommandHdl);
  51.     DisposeHandle(CommandHdl);
  52. END;
  53.  
  54.  
  55. {*------ DrawStuff -----------------------------------------------------------------*}
  56.  
  57. {**
  58.  **      DrawStuff will send some PostScript that draws gray text to a printer
  59.  **        that talks PostScript. It will also send the correct QuickDraw
  60.  **        representation of this to printers that do not talk PostScript.
  61.  **}
  62.  
  63.  PROCEDURE DrawStuff (boundsRect : Rect; theGPort : GrafPtr);
  64.  
  65.  VAR
  66.    oldPort  :     GrafPtr;
  67.    fNum        :    Integer;
  68.    rgb        :    RGBColor;
  69.    
  70.  BEGIN
  71.  
  72.     GetPort (oldPort);
  73.     SetPort (theGPort);
  74.  
  75.     PenSize(0, 0);
  76.     MoveTo(10, 10); 
  77.     Line(0, 0);
  78.     PenSize(1, 1);
  79.     
  80.     {**
  81.      ** This line tells the LaserWriter driver to ignore Quickdraw calls until it    
  82.      ** receives a PostScriptEnd picture comment.  This line is ignored by Quickdraw 
  83.      ** printer drivers (ie. the ImageWriter driver).
  84.      **}
  85.     
  86.     PicComment (PostScriptBegin, 0, NIL);
  87.  
  88.         {**
  89.          ** QuickDraw representation of the document. These calls
  90.          ** will only be executed by QuickDraw printers.
  91.          **}
  92.     
  93.     GetFNum('Times', fNum);        (* Set the font to Times Bold Italic 96 pt. *)
  94.     TextFont(fNum);
  95.     TextSize(96);
  96.     TextFace([bold, italic]);
  97.  
  98.     rgb.red := 52428;            (* 80% of 65535. *)
  99.     rgb.green := 52428;
  100.     rgb.blue := 52428;
  101.     RGBForeColor(rgb);            (* Set an 80% gray pen. *)
  102.     MoveTo(10, 100);            (* Move and draw. *)
  103.     DrawString('Sample Text.');
  104.     ForeColor(blackColor);        (* Reset foreground color. *)
  105.  
  106.         {**
  107.          ** PostScript representation of the document. These calls
  108.          ** will only be executed by PostScript printers.
  109.          **}
  110.  
  111.     SendPostScript('0 760 translate 1 -1 scale');
  112.     SendPostScript('/Times-BoldItalic findfont 96 scalefont setfont');
  113.     SendPostScript('10 660 moveto .8 setgray (Sample Text.) show');
  114.          
  115.     {**
  116.      ** This comment tells the LaserWriter driver tro start executing 
  117.      ** QuickDraw calls normally.
  118.      **}
  119.  
  120.     PicComment (PostScriptEnd, 0, NIL);
  121.     SetPort(oldPort);
  122.  
  123.  END;  {**  DrawStuff  **}
  124.  
  125.  
  126.  
  127.  
  128. {*------ PrintStuff ----------------------------------------------------------------*}
  129. {**
  130.  **        PrintStuff will call all of the necessary Print Manager calls to print 
  131.  **        a document. It checks PrError() after each Print Manager call. If an error 
  132.  **     is found, all of the Print Manager open calls (i.e. PrOpen, PrOpenDoc...) 
  133.  **        will have a corresponding close call before the error is posted to the user. 
  134.  **        You want to use this approach to make sure the Print Manager closes properly 
  135.  **        and all temporary memory is released.
  136.  **}
  137.  
  138. PROCEDURE PrintStuff;
  139.  
  140. VAR
  141.   oldPort              : GrafPtr;
  142.   thePrRecHdl        : THPrint;
  143.   thePrPort            : TPPrPort;
  144.   theStatus            : TPrStatus;
  145.     
  146. BEGIN
  147.    GetPort(oldPort);
  148.     
  149.    thePrRecHdl := THPrint(NewHandle(SIZEOF(TPrint)));
  150.     
  151.    IF (MemError = noErr) AND (thePrRecHdl <> NIL) THEN
  152.     BEGIN
  153.        PrOpen;
  154.        IF (PrError = noErr) THEN
  155.         BEGIN
  156.            PrintDefault(thePrRecHdl);
  157.  
  158.            IF (PrError = noErr) THEN
  159.             BEGIN
  160.                IF (PrStlDialog(thePrRecHdl)) THEN
  161.                 BEGIN
  162.                    IF (PrJobDialog(thePrRecHdl)) THEN 
  163.                     BEGIN
  164.                           thePrPort := PrOpenDoc(thePrRecHdl, NIL, NIL);
  165.  
  166.                       IF (PrError = noErr) THEN
  167.                         BEGIN
  168.  
  169.                              PrOpenPage(thePrPort, NIL);
  170.                                 
  171.                           IF (PrError = noErr) THEN
  172.                             BEGIN
  173.                               {**
  174.                                   rPage (IM II-150) is the printable area for the  
  175.                                   currently selected printer. By passing the current  
  176.                                   port to the draw routine, enables your app
  177.                                   to use the same routine to draw to the screen
  178.                                   and the printer's GrafPort.
  179.                                **}
  180.                                     
  181.                                DrawStuff (thePrRecHdl^^.prInfo.rPage, 
  182.                                            GrafPtr (thePrPort));
  183.                                  
  184.                              END;
  185.                             PrClosePage(thePrPort);
  186.                           END;
  187.                              
  188.                           PrCloseDoc(thePrPort);
  189.                              
  190.                           IF (thePrRecHdl^^.prJob.bJDocLoop = bSpoolLoop) and (PrError = noErr) THEN
  191.                                PrPicFile(thePrRecHdl, NIL, NIL, NIL, @theStatus);
  192.  
  193.                       END;
  194.                   END;
  195.               END;
  196.           END;
  197.         
  198.         PrClose;
  199.  
  200.      END;
  201. END;  {**  PrintStuff  **}
  202.  
  203.  
  204. {*------ main ----------------------------------------------------------------------*}
  205.  
  206. BEGIN
  207.     
  208.     InitGraf(@qd.thePort);
  209.     InitFonts;
  210.     FlushEvents(everyEvent, 0);    
  211.     InitWindows;
  212.     InitMenus;
  213.     TEInit;
  214.     InitDialogs(NIL);
  215.     InitCursor;
  216.  
  217.     PrintStuff;
  218.  
  219. END. {**  main  **}